package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.exceptions.OpcBizException;
import com.csun.cmny.provider.mapper.OpcFlowMessageGroupMapper;
import com.csun.cmny.provider.model.domain.OpcFlowMessage;
import com.csun.cmny.provider.model.domain.OpcFlowMessageGroup;
import com.csun.cmny.provider.model.domain.OpcFlowMessageGroupWay;
import com.csun.cmny.provider.model.dto.message.FlowMessageQueryDto;
import com.csun.cmny.provider.model.vo.message.FlowMessageVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OpcFlowMessageGroupService;
import com.csun.cmny.provider.service.OpcFlowMessageService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-11-07 11:05
 * @Description:
 */
@Service
@Transactional
public class OpcFlowMessageGroupServiceImpl extends BaseService<OpcFlowMessageGroup> implements OpcFlowMessageGroupService {


    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OpcFlowMessageService opcFlowMessageService;
    @Resource
    private OpcFlowMessageGroupMapper opcFlowMessageGroupMapper;

    /**
     * bind omc flow message with group
     *
     * @param opcFlowMessageGroup the omc message group
     * @return the int
     */
    @Override
    public Integer bind(OpcFlowMessageGroup opcFlowMessageGroup) {

        Long flowMessageId = opcFlowMessageGroup.getFlowMessageId();
        Long groupId = opcFlowMessageGroup.getGroupId();

        Preconditions.checkArgument(PublicUtil.isNotEmpty(flowMessageId),
                cmnyMessageSource.getMessage("flow.message.id.null", null));

        Preconditions.checkArgument(PublicUtil.isNotEmpty(groupId),
                cmnyMessageSource.getMessage("group.id.null", null));

        OpcFlowMessage opcFlowMessage = opcFlowMessageService.selectByKey(flowMessageId);
        if (PublicUtil.isEmpty(opcFlowMessage)) {
            throw new OpcBizException(ErrorCodeEnum.OMC10031402);
        }

        opcFlowMessageGroup.setMessage(opcFlowMessage.getMessage());
        opcFlowMessageGroup.setParam(opcFlowMessage.getParam());
        opcFlowMessageGroup.setRole(opcFlowMessage.getRole());
        opcFlowMessageGroup.setTitle(opcFlowMessage.getTitle());
        opcFlowMessageGroup.setShow(opcFlowMessage.getShow());
        opcFlowMessageGroup.setUrl(opcFlowMessage.getUrl());

        return opcFlowMessageGroupMapper.insertSelective(opcFlowMessageGroup);
    }

    /**
     * untied omc flow message with group
     *
     * @param opcFlowMessageGroup the omc flow message group
     *
     * @return the int
     */
    @Override
    public Integer untied(OpcFlowMessageGroup opcFlowMessageGroup) {

        Long flowMessageId = opcFlowMessageGroup.getFlowMessageId();
        Long groupId = opcFlowMessageGroup.getGroupId();

        Preconditions.checkArgument(PublicUtil.isNotEmpty(flowMessageId),
                cmnyMessageSource.getMessage("flow.message.id.null", null));

        Preconditions.checkArgument(PublicUtil.isNotEmpty(groupId),
                cmnyMessageSource.getMessage("group.id.null", null));

        return opcFlowMessageGroupMapper.deleteByPrimaryKey(opcFlowMessageGroup);
    }

    /**
     * edit omc flow message group
     *
     * @param opcFlowMessageGroup the omc flow message group
     * @return the int
     */
    @Override
    public Integer edit(OpcFlowMessageGroup opcFlowMessageGroup) {

        Long flowMessageId = opcFlowMessageGroup.getFlowMessageId();
        Long groupId = opcFlowMessageGroup.getGroupId();

        Preconditions.checkArgument(PublicUtil.isNotEmpty(flowMessageId),
                cmnyMessageSource.getMessage("flow.message.id.null", null));

        Preconditions.checkArgument(PublicUtil.isNotEmpty(groupId),
                cmnyMessageSource.getMessage("group.id.null", null));

        return opcFlowMessageGroupMapper.updateByPrimaryKeySelective(opcFlowMessageGroup);
    }

    /**
     * query omc flow message group list with page
     *
     * @param flowMessageQueryDto the flow message query dto
     *
     * @return the list
     */
    @Override
    public List<FlowMessageVo> queryListWithPage(FlowMessageQueryDto flowMessageQueryDto) {

        PageHelper.startPage(flowMessageQueryDto.getPageNum(), flowMessageQueryDto.getPageSize());

        return opcFlowMessageGroupMapper.queryListWithPage(flowMessageQueryDto);
    }

    /**
     * query flow message by code and group id
     *
     * @param code    the code
     * @param groupId the group id
     *
     * @return the flow message vo
     */
    @Override
    public FlowMessageVo queryByCodeAndGroupId(String code, Long groupId) {

        return opcFlowMessageGroupMapper.queryByCodeAndGroupId(code, groupId);
    }

    @Override
    public List<Long> queryMessageIdListByGroupId(Long groupId) {

        return opcFlowMessageGroupMapper.queryMessageIdListByGroupId(groupId);
    }

    /**
     * @param opcFlowMessageGroupWay
     * @return
     */
    @Override
    public List<Long> queryMessageWayIdList(OpcFlowMessageGroupWay opcFlowMessageGroupWay) {

        return opcFlowMessageGroupMapper.queryMessageWayIdList(opcFlowMessageGroupWay);
    }

}
